//
//  NSMutableArray+Safe.m
//  CommonTool
//
//  Created by Name i on 2022/4/13.
//

#import "NSMutableArray+Safe.h"

@implementation NSMutableArray (Safe)

+ (void)exchangeMethod {

    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        
        Class targetClass = NSClassFromString(@"__NSArrayM");
        
        // objectAtIndex
        [NSObject tnSwizzlingInstanceMethod: @selector(objectAtIndex:)
                                 withMethod: @selector(safeMutable_objectAtIndex:)
                                  withClass:targetClass];
        
        // getObjects:range:
        [NSObject tnSwizzlingInstanceMethod: @selector(getObjects:range:)
                                 withMethod: @selector(safeMutable_getObjects:range:)
                                  withClass: targetClass];

        // objectAtIndexedSubscript:
        [NSObject tnSwizzlingInstanceMethod: @selector(objectAtIndexedSubscript:)
                                 withMethod: @selector(safeMutable_objectAtIndexedSubscript:)
                                  withClass: targetClass];

        // removeObjectsInRange
        [NSObject tnSwizzlingInstanceMethod: @selector(removeObjectsInRange:)
                                 withMethod: @selector(safeMutable_removeObjectsInRange:)
                                  withClass: targetClass];
        // insertObject:atIndex:
        [NSObject tnSwizzlingInstanceMethod: @selector(insertObject:atIndex:)
                                 withMethod: @selector(safeMutable_insertObject:atIndex:)
                                  withClass: targetClass];
        // removeObject:inRange:
        [NSObject tnSwizzlingInstanceMethod: @selector(removeObject:inRange:)
                                 withMethod: @selector(safeMutable_removeObject:inRange:)
                                  withClass: targetClass];
    });
}

- (void)safeMutable_getObjects:(id  _Nonnull __unsafe_unretained [])objects range:(NSRange)range {
    @try {
        [self safeMutable_getObjects:objects range:range];
    } @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    } @finally {
        
    }
}

- (id)safeMutable_objectAtIndex: (NSUInteger)index {
    id object = nil;
    @try {
        object = [self safeMutable_objectAtIndex:index];
    } @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    } @finally {
        return object;
    }
}

- (void)safeMutable_removeObjectsInRange: (NSRange)range {
    @try {
        [self safeMutable_removeObjectsInRange:range];
    } @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    } @finally {

    }
}

- (void)safeMutable_removeObject:(id)anObject inRange:(NSRange)range {
    @try {
        [self safeMutable_removeObject:anObject inRange:range];
    } @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    } @finally {
        
    }
}

- (void)safeMutable_insertObject:(id)anObject atIndex:(NSUInteger)index {
    @try {
        [self safeMutable_insertObject:anObject atIndex:index];
    } @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultIgnore];
    } @finally {
        
    }
}

- (id)safeMutable_objectAtIndexedSubscript:(NSUInteger)index {
    id object = nil;
    @try {
        object = [self safeMutable_objectAtIndexedSubscript:index];
    } @catch (NSException *exception) {
        [NNDefender crashWithException:exception defaultToDo:    NNDefenderDefaultReturnNil];
    } @finally {
        return object;
    }
}

@end
